//
//  MoreViewController.m
//  HundPark
//
//  Created by Aguilar Rolando on 2013-03-14.
//  Copyright (c) 2013 ARV IT. All rights reserved.
//

///README:   DETTA ÄR LAB. Inget som  används i nuläget. Ignorerade MoreViewController.h och MoreViewController.m
#import "MoreViewController.h"
@interface MoreViewController ()
{
    BOOL pageControlBeingUsed;
}

@property (weak, nonatomic) City *allParks;
@property (weak, nonatomic) NSString *UUIDString;
@property (strong, nonatomic) NSMutableData *receivedData;
@property (weak, nonatomic) IBOutlet UIImageView *parkImage;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

- (IBAction)changePage:(id)sender;
- (IBAction)getImage:(UIButton *)sender;

@end

@implementation MoreViewController

@synthesize allParks = _allParks;
@synthesize scrollView = _scrollView;
@synthesize UUIDLabel = _UUIDLabel;
@synthesize receivedData = _receivedData;
@synthesize activityIndicator = _activityIndicator;

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (!pageControlBeingUsed) {
        // Switch the indicator when more than 50% of the previous/next page is visible
        CGFloat pageWidth = self.scrollView.bounds.size.width;
        int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        self.pageControl.currentPage = page;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
    uint page = sender.contentOffset.x / self.scrollView.bounds.size.width;
    [self.pageControl setCurrentPage:page];
    pageControlBeingUsed = NO;

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    pageControlBeingUsed = NO;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //Get allParks value that was loaded at start
    AppDelegate *appDelegate =  (AppDelegate *)[[UIApplication sharedApplication ]delegate];
    self.allParks = appDelegate.allParks;
    
    
    [self.scrollView setDelegate:self];
    self.pageControl.numberOfPages = 3;
    [self getImageFromFileServer];
    
    //Load from NSUserDeafults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSString *UUIDString = [defaults objectForKey:@"appUUIDKey"];
    self.UUIDLabel.text = UUIDString;
}



- (IBAction)changePage:(id)sender{
    // update the scroll view to the appropriate page
    CGRect frame;
    frame.origin.x = self.scrollView.bounds.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.bounds.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];
    pageControlBeingUsed = YES;
}

- (void)getImageFromFileServer {    

    NSURL *url = [NSURL URLWithString:@"http://hundapp.byethost8.com/parkImages/desiredodo.png"];
    [self.activityIndicator startAnimating];
    
    AFImageRequestOperation* imageOperation =
    [AFImageRequestOperation imageRequestOperationWithRequest: [NSURLRequest requestWithURL:url]
                                                      success:^(UIImage *image) {
                                                          //create an image view, add it to the view
                                                          /*
                                                          UIImageView* thumbView = [[UIImageView alloc] initWithImage: image];
                                                          thumbView.frame = CGRectMake(0,0,90,90);
                                                          thumbView.contentMode = UIViewContentModeScaleAspectFit;
                                                           */
                                                          self.parkImage.image = image;
                                                          [self.activityIndicator stopAnimating];
                                                      }];
    
    NSOperationQueue* queue = [[NSOperationQueue alloc] init];
    [queue addOperation:imageOperation];
}

- (IBAction)getImage:(UIButton *)sender {
    [self getImageFromFileServer];
}


@end
